package sdfi.lee.Listener;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Application Lifecycle Listener implementation class MyListener
 *
 */
public class MyListener implements ServletContextListener {
	
	
	private Connection con=null;
	private Session session=null;
	private MessageConsumer consumer=null;
	private MessageProducer producer=null;
    /**
     * Default constructor. 
     */
    public MyListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    	
    	try
    	{
    		this.start();
    	}
    	catch(Exception ee)
    	{
    		ee.printStackTrace();
    	}
    }
	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        //  Auto-generated method stub
    	try
    	{
    		this.con.close();
    	}
    	catch(Exception ee)
    	{
    		ee.printStackTrace();
    	}
    }
    public void start() throws Exception 
    {	
    	System.out.println("监听.....");

    	 con=sdfi.lee.JMS.ConnectionFactory.getCon();
    	 con.start();
    	
    	 session=con.createSession(false,Session.AUTO_ACKNOWLEDGE);
    	 consumer=session.createConsumer(session.createQueue("qa"));
    	 consumer.setMessageListener(new MyMessageListener(con.createSession(false, Session.AUTO_ACKNOWLEDGE)));
    	 
    }
    
	
}
class MyMessageListener implements MessageListener{
		Session session=null;
		public MyMessageListener(Session session)
		{
			this.session=session;
			
		}
		@Override
		public void onMessage(Message msg) {
			// TODO Auto-generated method stub
			try
			{
				String queue="";
				MapMessage objmsg=(MapMessage)msg;
				//Question question=(Question)objmsg.getObject();
				if(objmsg.getMapNames().hasMoreElements())
				queue=(String)objmsg.getMapNames().nextElement();
				 String question=objmsg.getString(queue);
				 question=question.substring(0,question.indexOf("#"));
				//��Ӧ����
				this.charge(question,queue);
			}
			catch(Exception ee)
			{
				ee.printStackTrace();
			}
		}
		private void charge(String question,String queue)throws Exception
		{
			MessageProducer producer=session.createProducer(session.createQueue(queue));
			MapMessage map=null;
			map=session.createMapMessage();
			for(int i=0;i<10;i++)
			{
				
				map.setString(Integer.valueOf(i).toString()+question,
						"z:/achievement/济南高新技术产业开发区/欢迎访问济南高新技术产业开发区技术成果53504.txt" +"#<a href='#'>黄建华</a>呵呵.......");
			}
			//map.setString(question, "<a href='#' class='location'>黄建华</a>");
//			/*
			for(int i=0;i<1;i++)
			{
				
				map.setString(Integer.valueOf(i).toString()+question,
						"z:/journals/中国药房/青霉素诱发18例癫痫样发作回顾性分析" +"#<a href='#'>石磊</a>呵呵.......");
			}
//			*/
//			/*
			map.setString(question, "<a href='#' class='location'>袁正敏</a><a href='#' class='location'>石磊</a>");
//			*/
			producer.send(map);
		//	producer.send(session.createObjectMessage(list));
		
			
			
		}
		
	
}
